// pages/xiaofeibi/xiaofeibi.js
const app=getApp();
//获得全局js变量
var util = require('../../utils/util.js');
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
        navIndex: 1,
        cateid: 1,
        page: 0,
        imgurl: app.data.imgurl,
        lists: [],
        catelist: [],
        keyword: ''
	},
    //搜索框
    bindinput: function (e) {
        this.setData({
            keyword: e.detail.value
        });
        
        if (this.data.keyword.length <= 0) {
            //return;
        }
        //存储搜索记录
        var searchData = wx.getStorageSync('searchData') || []
        if (this.data.keyword.length > 0){
            searchData.push(e.detail.value)
        }
        wx.setStorageSync('searchData', searchData)

        this.setData({
            page: 0,
            lists: [],
        });
        this.getgoods();
    },
    //获取分类
    cateinfo: function () {
        var that = this;
        util.httpRrquest(app.data.rooturl + 'mall/getGoodCate', 'GET', {  })
        .then(function (response) {
            that.setData({
                catelist: response.data.data
            })
            console.log(that.data.catelist)
        }, function (error) {
            console.log(error);
        });
    },
    //获取商品列表
    getgoods: function () {
        var that = this;
        var page = that.data.page;
        page++;
        that.setData({
            page: page
        })
        util.httpRrquest(app.data.rooturl + 'mall/getGoodList', 'GET', { cateId: that.data.cateid, page: page, keyword: that.data.keyword })
        .then(function (response) {
            if (that.data.page == 1) {
                if (response.data.data.length > 0) {
                    that.setData({
                        lists: response.data.data
                    })
                } else {
                    wx.showToast({
                        title: '暂无数据...',
                        icon: 'loading'
                    })
                }
            } else {
                if (response.data.data.length > 0) {
                    var lists = that.data.lists;
                    that.setData({
                        lists: lists.concat(response.data.data)
                    })
                } else {
                    wx.showToast({
                        title: '暂无更多数据...',
                        icon: 'loading'
                    })
                }
            }
        }, function (error) {
            console.log(error);
        });
    },
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function (options) {
		
        
	},
	
	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function () {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function () {
		if (wx.getStorageSync('searchkeyword')){
            console.log(this.data.keyword);
            this.setData({
                keyword: wx.getStorageSync('searchkeyword')
            });
            wx.removeStorageSync('searchkeyword')
        }else{
            this.setData({
                keyword: ''
            });
            console.log(this.data.keyword);
        }     
        if (wx.getStorageSync('fenleiid')){
            this.setData({
                cateid: wx.getStorageSync('fenleiid'),
                navIndex: wx.getStorageSync('fenleiid'),
            });
            wx.removeStorageSync('fenleiid')
        }
        this.setData({
            page:0
        }); 
        //获取提货分类
        this.cateinfo();
        //获取商品列表
        this.setData({
            lists:[]
        });
        this.getgoods();
	},
    navTab(e){
        let i = e.currentTarget.dataset.id
        this.setData({
            navIndex: i,
            page: 0,
            lists: [],
            cateid:i
        })
        this.getgoods();
    },
    //商品详情
    goodinfo:function(e){
        var id=e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '/pages/goodDetail/goodDetail?id=' + id,
        })
    },
	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function () {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function () {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function () {
		this.setData({
            page: 0,
            lists: [],
        })
        this.getgoods();
	},
	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function () {
		this.getgoods();
	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function () {
		var shareinfo = app.setShareMessage();
        console.log(shareinfo);
        return {
            title: shareinfo.title,
            path: shareinfo.path,
            imageUrl: shareinfo.imageUrl,
            success: (res) => {
                wx.showToast({
                    title: "分享成功",
                    icon: 'loading'
                })
            }
        }
	}
})